package com.googlecode.tidej.client.editor;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.TextBox;
import com.googlecode.tidej.client.Dialog;
import com.googlecode.tidej.client.TidejController;
import com.googlecode.tidej.shared.model.Property;
import com.googlecode.tidej.shared.model.Type;

public class PropertySignatureDialog extends Dialog {
  Type owner;
  Property property;
  TextBox nameBox = new TextBox();
  TidejController controller;
  Button deleteButton = new Button("Delete");
  
  public PropertySignatureDialog(TidejController controller) {
    super("");
    this.controller = controller;
    FlexTable table = new FlexTable();
    table.setHTML(0, 0, "<b>Property Name</b>");
    table.setWidget(0, 1, nameBox);
    getContentPanel().add(table);
    Button okButton = new Button("Ok");
    okButton.addClickHandler(new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        commit();
      }
    });
    deleteButton.addClickHandler(new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        delete();
      }
    });

    addButton(deleteButton);
    addButton(okButton);
  }
  
  public void createProperty(Type owner) {
    this.owner = owner;
    deleteButton.setVisible(false);
    setText("Create New Property");
    show();
    center();
  }
  
  public void editProperty(Property property) {
    this.property = property;
    setText("Edit Property");
    deleteButton.setVisible(true);
    owner = property.getOwner();
    nameBox.setText(property.getName());
    show();
    center();
  }
  
  public void delete() {
    owner.removeProperty(property);
    controller.saveCode(owner);
    controller.select(owner);
    hide();
  }
  
  public void commit() {
    String name = nameBox.getText();
    if (!Dialog.checkName("Name", name)) {
      return;
    }
    if (owner.getOperation(name) != null) {
      Window.alert("Operation with the same name exists already.");
    }
    if (owner.getProperty(name) != null && owner.getProperty(name) != property) {
      Window.alert("Property with the same name exists already.");
    }
    String documentation = null;
    Type type = null;
    String initialValue = null;
    if (property != null) {
      documentation = property.getDocumentation();
      type = property.getType();
      initialValue = property.getInitialValue();
      owner.removeProperty(property);
    }
    property = new Property(name, type, false, initialValue);
    property.setDocumentation(documentation);
    owner.addProperty(property);
    controller.saveCode(owner);
    controller.select(property);
    hide();
  }
}
